package org.budo.ocr.service.baidu;

import org.budo.ocr.service.api.BudoOcrService;
import org.budo.support.assertion.Assert;

import com.baidu.aip.ocr.AipOcr;

import lombok.Getter;
import lombok.Setter;

/**
 * @author limingwei
 */
@Getter
@Setter
public abstract class AbstractBaiduAiOcrService implements BudoOcrService {
    private String appId;

    private String apiKey;

    private String secretKey;

    private AipOcr aipOcr;

    protected AipOcr getAipOcr() {
        if (null != this.aipOcr) {
            return this.aipOcr;
        }

        synchronized (AbstractBaiduAiOcrService.class) {
            if (null != this.aipOcr) {
                return this.aipOcr;
            }

            String appId = this.getAppId();
            Assert.notEmpty(appId, "#31 appId is null, this=" + this);

            String apiKey = this.getApiKey();
            Assert.notEmpty(apiKey, "#34 apiKey is null, this=" + this);

            String secretKey = this.getSecretKey();
            Assert.notEmpty(secretKey, "#37 secretKey is null, this=" + this);

            return this.aipOcr = new AipOcr(appId, apiKey, secretKey);
        }
    }

    @Override
    public String toString() {
        return super.toString() //
                +", apiKey="+ this.getApiKey() //
                + ", appId="+this.getAppId();
    }
}